<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="日期范围">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" allow-clear />
        </a-form-item>
        <a-form-item label="姓名" class="w-[300px]">
          <SelectTable
            v-model:value="formState.brName"
            type="患者"
            :search-params="{ stdate: dayjs().subtract(7, 'day').format('YYYY-MM-DD'), eddate: dayjs().format('YYYY-MM-DD') }"
            custom-input
            empty-close
            :maxlength="20"
            @select="onSelectPatient"
          />
        </a-form-item>
        <!-- <a-form-item name="txnr" label="" class="ml-4">
          <a-input v-model:value="formState.txnr" class="w-64" placeholder="通信内容" allow-clear @keydown.enter="getData" />
        </a-form-item>
        <a-form-item name="txlx" label="通信类型" class="ml-4 w-72">
          <a-select v-model:value="formState.txlx" class="w-64" placeholder="请选择">
            <a-select-option value="2102">【2102】药店结算</a-select-option>
            <a-select-option value="2103">【2103】药店结算撤销</a-select-option>
            <a-select-option value="2207">【2207】门诊结算</a-select-option>
            <a-select-option value="2208">【2208】门诊结算撤销</a-select-option>
            <a-select-option value="2304">【2304】住院结算</a-select-option>
            <a-select-option value="2207">【2207】住院结算撤销</a-select-option>
            <a-select-option value="2401">【2401】入院办理</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item name="planName" label="" class="ml-4">
          <a-button type="primary" @click="readCard">读取医保卡</a-button>
        </a-form-item> -->
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2" type="primary" @click="openAddModal"> 自填冲正 </a-button>
      </div>
      <BaseTable
        fill
        :data-source="tableData"
        :loading="loading"
        :custom-row="(record) => ({ onClick: () => onSelectRow(record) })"
        :row-class-name="(record) => (record.id === selectRow.id ? 'selected-row' : '')"
        column-code="000564"
        :pagination="pagination"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <BaseButton confirm-text="是否确认冲正交易?" type="link" size="small" @click="onOk(record)"> 冲正交易 </BaseButton>
              <a-button type="link" size="small" @click="openParamsModal(record)">参数</a-button>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @ok="getData" />
    <Parameter v-model:open="paramsOpen" :row-data="rowData" @ok="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseButton from '@/components/BaseButton'
import { post } from '@/utils/request'
import Edit from './Edit.vue'
import Parameter from './parameter'

// const printJson = ref('')
// const jsonStr = ref('')
// const jsonStr = ref(
//   '{"ybkRquese":{"cardtype":"1","carddata":""},"ybkResonse":{"xb":"女","sfzh":"310104195701174561","lxdh":"65537984","txdz":"淮海中路1799号","yzbm":"200233","klbbz":"1","kh":"D06073096","xm":"张美芳","zhbz":"2000000000000000","dnzhye":0.0,"lnzhye":0.0,"mzzfdxjzfljs":300.0,"zyzfxxzfljs":1200.0,"mzzfdde":300.0,"zyzfdde":1200.0,"tczffdxde":610000.0,"qfxsfdxxfylj":24274.97,"jlch":"100806419510","mzylbkdd":"","br_id":"D06073096","xzqh":"","ybsqjmbz":"1"}}'
// )
const patientState = ref({})
const rowData = ref(null)
const formRef = ref(null)
const editOpen = ref(false)
const paramsOpen = ref(false)
const selectRow = ref({})
const formState = ref({
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
  // txnr: '',
  // txlx: undefined
})

const onSelectPatient = (select) => {
  formState.value.brName = select.brName
  formState.value.brId = select.brId
}
// /outp/oupCharge/getOutpInterfaceLog
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/oupCharge/get2207InterfaceLog',
  immediate: true,
  params: formState.value,
  formatParams: () => {
    return {
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      brId: formState.value.brName ? formState.value.brId : ''
      // txnr: params.txnr,
      // txlx: params.txlx
    }
  },
  formatResult: (res) => {
    selectRow.value = {}
    rowData.value = {}
    return res
  }
})
// const readCard = async () => {
//   // jsonStr.value = window.openExe?.readSFZ(printJson.value) //调用壳
//   const res = await post('/nationalMedicalInsurance/nmiCommon/getPatientlnfo', {
//     jsonStr: jsonStr.value,
//     mdtrtCertNo: '',
//     mdtrtCertType: '03'
//   })
//   if (res.code !== 0) return message.error(res.msg)
//   patientState.value = res.data?.baseinfo || {}
// }
const openParamsModal = (record) => {
  rowData.value = Object.assign({}, record)
  paramsOpen.value = true
}
const openAddModal = () => {
  // if (!selectRow.value.id) return message.error('请至少选择一项')
  editOpen.value = true
}
const onSelectRow = (record) => {
  selectRow.value = record
  rowData.value = record
}

const onOk = async (record) => {
  if (!patientState.value.psn_no) return message.error('请读取医保卡')
  const { code, msg } = await post('/outp/oupCharge/doPunchUp', {
    psnNo: patientState.value.psn_no,
    omsgid: record.msgid,
    oinfno: record.interfacename
  })
  if (code !== 0) return message.error(msg || '冲正失败')
  getData()
  message.success('冲正成功')
}
// const getJson = async () => {
//   const res = await post('/nationalMedicalInsurance/nmiCommon/getRegistrationBillPrint', { businessType })
//   if (res.code !== 0) return message.error(res.msg)
//   printJson.value = res.data
// }
</script>
<style></style>
